#include<cstdio>
#include<cstring>
const int maxn=11000;
int p[maxn],sum[maxn],trans[maxn];
int t,n,q;
int find(int x)
{
	int t;
	if(p[x]!=x)
	{
		t=p[x];
		p[x]=find(p[x]);
		trans[x]=trans[x]+trans[t];
	}
	return p[x];
}
void merge(int x,int y)
{
	int p1=find(x);
	int p2=find(y);
	p[p1]=p2;
	trans[p1]=1;
	sum[p2]+=sum[p1];
}
int main()
{
	scanf("%d",&t);
	for(int cas=1;cas<=t;cas++)
	{
		printf("Case %d:\n",cas);
		scanf("%d%d",&n,&q);
		for(int i=1;i<=n;i++)
		{
			p[i]=i;sum[i]=1;trans[i]=0;
		}
		for(int i=1;i<=q;i++)
		{
			char ch;
			int a,b;
			scanf("%c%c",&ch,&ch);
			if(ch=='Q')
			{
				scanf("%d",&a);
				b=find(a);

				printf("%d %d %d\n",b,sum[b],trans[a]);
			}
			else
			{
				scanf("%d%d",&a,&b);
				merge(a,b);
			}
		}
	}
	return 0;
}
